<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/07/10 08:57:50 $)
// $RCSfile: client_knowledgebase.php,v $ : $Revision: 1.18 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/knowledgebase/functions_knowledgebase.php");
require_once ("./modules/knowledgebase/functions_clientkb.php");
require_once ("./includes/functions_users.php");
require_once ("./includes/functions_comments.php");
$template->loadLanguageSection("knowledgebase");

// Pre Processing
if ($eventaction == "view")
{
	$eventaction = "catrender";
}

// ======= PERMISSION CHECK =======
if ($_SWIFT["user"]["permissions"]["perm_canviewknowledgebase"] == "0" || $_SWIFT["settings"]["kb_enableclient"] == "0")
{
	$template->assign("errormessage", $_SWIFT["language"]["perminvalid"]);
	echo $template->displayTemplate("header");
	echo $template->displayTemplate("footer");
	exit;
}

/**
* ###############################################
* The Category Display
* ###############################################
*/
if ($eventaction == "catrender")
{
	// Cache the templates before hand
	$template->cache(array('kbcatdisplay', 'kbnavbar', 'header', 'footer', 'navbar'));

	if (empty($_GET["nav"]))
	{
		$_nav = "0";
	} else {
		$_nav = htmlspecialchars($_GET["nav"]);
	}
	if ($_GET["parentcategoryid"] != 0)
	{
		$_nav .= ",".intval(htmlspecialchars($_GET["parentcategoryid"]));
	}

	// First get the parent category
	if (empty($_GET["parentcategoryid"]))
	{
		$parentcategoryid = 0;
	} else {
		$parentcategoryid = intval($_GET["parentcategoryid"]);
	}

	// Now that we have the parent category, make sure it belongs to the user group of the current user
	if (!checkCatTemplateGroup($parentcategoryid))
	{
		trigger_error($_SWIFT["language"]["catpermerror"], E_USER_ERROR);
	}

	// Now that we have the parent category, build the list of categories under this parent category
	$funcresult = getSubCategories($parentcategoryid);
	$categorylist = $funcresult["categorylist"];
	$kbcatarticlelist = $funcresult["kbarticlelist"];
	$parentkbarticlelist = $funcresult["parentkbarticlelist"];
	$splitcatlist = $funcresult["splitcatlist"];
	$template->assign("nav", $_nav);
	if (count($categorylist))
	{
		$template->assign("showcategories", "1");
		$template->assign("categoryrowcount", ceil(count($categorylist)/2));
		$template->assign("splitcatlist", $splitcatlist);
		$template->assign("categorylist", $categorylist);
		$template->assign("kbcatarticlelist", $kbcatarticlelist);
	}

	$kbcatlist = array();
	$dbCore->query("SELECT kbcategories.* FROM `". TABLE_PREFIX ."kbcategories` AS kbcategories LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbcategories.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND kbcategories.parentcategoryid = '0' ORDER BY kbcategories.displayorder ASC;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["categorytype"] == SWIFTPUBLIC)
		{
			$kbcatlist[] = $dbCore->Record;
		}
	}

	if (count($kbcatlist))
	{
		$template->assign("inckbnav", true);
		$template->assign("kbnavcatlist", $kbcatlist);
	}

	// Ok now thats for the categories, right below the categories we also need to show any articles that might be there
	if (count($parentkbarticlelist))
	{
		$template->assign("showarticlelist", "1");
		$template->assign("parentkbarticlelist", $parentkbarticlelist);
	}

	// Done with the data processing, now its time to render the stuff out
	if (defined("ISMODERNBILLV5"))
	{
		global $CONF;
		$template->assign("modernbill", array("actionid" => getActionID("ShowFAQs"), "sessionid" => $CONF["user"]["sessionID"]));
		$template->assign("navigation", '<a href="dashboard.php?action='. getActionID("ShowDashboard") .'&sid='. $CONF["user"]["sessionID"] .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="faqs.php?action='. getActionID("ShowFAQs") .'&sid='. $CONF["user"]["sessionID"] .'&_m=knowledgebase&_a=view" id="navlink">'.$_SWIFT["language"]["knowledgebase"]."</a>".buildNavLink($_nav));
		if ($parentcategoryid == 0)
		{
			$template->assign("backurl", 'dashboard.php?action='. getActionID("ShowDashboard") .'&sid='. $CONF["user"]["sessionID"]);
		} else {
			$template->assign("backurl", 'faqs.php?action='. getActionID("ShowFAQs") .'&sid='. $CONF["user"]["sessionID"] .'&_m=knowledgebase&_a=view&parentcategoryid='.intval($_GET["pcid"]));
		}
	} else {
		$template->assign("navigation", '<a href="'. iif(defined("ISMODERNBILL"), "user.php", "index.php") .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="'. iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=faq&_m=knowledgebase&_a=view", "index.php?_m=knowledgebase&_a=view") .'" id="navlink">'.$_SWIFT["language"]["knowledgebase"]."</a>".buildNavLink($_nav));
		if ($parentcategoryid == 0)
		{
			$template->assign("backurl", iif(defined("ISMODERNBILL"), "user.php", "index.php"));
		} else {
			$template->assign("backurl", iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=faq&_m=knowledgebase&_a=view&parentcategoryid=".intval($_GET["pcid"]), "index.php?_m=knowledgebase&_a=view&parentcategoryid=".intval($_GET["pcid"])));
		}
	}

	$template->assign("sectiontitle", $_SWIFT["language"]["kbcat"]);
	$template->assign("sectiondesc", $_SWIFT["language"]["kbcatdesc"]);
	$template->assign("pcid", intval($parentcategoryid));

	echo $template->displayTemplate("kbcatdisplay");









/**
* ###############################################
* Question Display
* ###############################################
*/
} else if ($eventaction == "viewarticle") {
	$template->cache(array('kbarticleview', 'kbarticlenavbar', 'header', 'footer', 'navbar', 'comments'));

	$_article = getArticle($_GET["kbarticleid"]);
	if (!$_article || $_article["articlestatus"] != "published" || !checkCatTemplateGroup($_article["kbcategoryid"]))
	{
		trigger_error($_SWIFT["language"]["articleerror"], E_USER_ERROR);
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."kbarticles` SET `views` = `views` + 1 WHERE `kbarticleid` = '". intval($_GET["kbarticleid"]) ."';");

	renderClientComments(COMMENT_KNOWLEDGEBASE, $_article["kbarticleid"]);

	$_article["contents"] = getProcessedHTML($_article["contents"]);
	$_article["date"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $_article["dateline"]);
	$_article["modified"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $_article["lastmodified"]);
	$_article["jssubject"] = addslashes($_article["subject"]);

	if (empty($_GET["nav"]))
	{
		$_nav = "0";
	} else {
		$_nav = $_GET["nav"];
	}
	$template->assign("docsubject", $_article["subject"]);
	if (defined("ISMODERNBILLV5"))
	{
		global $CONF;
		$template->assign("modernbill", array("actionid" => getActionID("ShowFAQs"), "sessionid" => $CONF["user"]["sessionID"]));
		$template->assign("navigation", '<a href="dashboard.php?action='. getActionID("ShowDashboard") .'&sid='. $CONF["user"]["sessionID"] .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="faqs.php?action='. getActionID("ShowFAQs") .'&sid='. $CONF["user"]["sessionID"] .'&_m=knowledgebase&_a=view" id="navlink">'.$_SWIFT["language"]["knowledgebase"]."</a>".buildNavLink($_nav).' &raquo; <a href="'. 'faqs.php?action='. getActionID("ShowFAQs") .'&sid='. $CONF["user"]["sessionID"] .'&&_m=knowledgebase&_a=viewarticle&kbarticleid='. intval($_article["kbarticleid"]) .'&nav='. urlencode($_nav) .'" id="navlink">'.$_article["subject"].'</a>');
	} else {
		$template->assign("navigation", '<a href="'. iif(defined("ISMODERNBILL"), "user.php", "index.php") .'" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="'. iif(defined("ISMODERNBILL"), "user.php?op=menu&tile=faq&_m=knowledgebase&_a=view", "index.php?_m=knowledgebase&_a=view") .'" id="navlink">'.$_SWIFT["language"]["knowledgebase"]."</a>".buildNavLink($_nav).' &raquo; <a href="'. iif(defined("ISMODERNBILL"), 'user.php?op=menu&tile=faq&_m=knowledgebase&_a=viewarticle&kbarticleid='. intval($_article["kbarticleid"]) .'&nav='. urlencode($_nav), 'index.php?_m=knowledgebase&_a=viewarticle&kbarticleid='. intval($_article["kbarticleid"]) .'&nav='. urlencode($_nav)) .'" id="navlink">'.$_article["subject"].'</a>');
	}

	$template->assign("article", $_article);
	$template->assign("articlenav", true);
	echo $template->displayTemplate("kbarticleview");











/**
* ###############################################
* Printable Version
* ###############################################
*/
} else if ($eventaction == "printable") {
	// First get the article data
	$_article = getArticle($_GET["kbarticleid"]);
	if (!$_article)
	{
		trigger_error($_SWIFT["language"]["articleerror"], E_USER_ERROR);
	}

	$_article["contents"] = getProcessedHTML($_article["contents"]);
	$_article["date"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $_article["dateline"]);

	$template->assign("article", $_article);
	echo $template->displayTemplate("kbprintableversion");









/**
* ###############################################
* PDF Export
* ###############################################
*/
} else if ($eventaction == "pdfexport") {
	$result = makeArticlePDF($_GET["kbarticleid"]);
	if (!$result)
	{
		trigger_error($_SWIFT["language"]["articleerror"], E_USER_ERROR);
	}









	
/**
* ###############################################
* Rating Change
* ###############################################
*/
} else if ($eventaction == "ratearticle") {
	$ratingresult = updateArticleRating($_GET["kbarticleid"], iif($_GET["ratetype"]==1, true, false));
	if (!$ratingresult)
	{
		trigger_error($_SWIFT["language"]["articleerror"], E_USER_ERROR);
	}

	if (defined("ISMODERNBILL"))
	{
		header("location: user.php?op=menu&tile=faq&_m=knowledgebase&_a=viewarticle&kbarticleid=".intval($_GET["kbarticleid"])."&ratingconfirm=1");
	} else if (defined("ISMODERNBILLV5")) {
		header("location: faqs.php?action=".intval($_GET["action"])."&sid=". $_GET["sid"] ."&_m=knowledgebase&_a=viewarticle&kbarticleid=". intval($_GET["kbarticleid"]) ."&nav=0");
	} else {
		header("location: ".$_SWIFT["swiftpath"]."index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=".intval($_GET["kbarticleid"])."&ratingconfirm=1");
	}
}
?>